package com.fate.framework.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.fate.framework.constant.SystemConst;
import com.fate.framework.dto.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

@Slf4j
@SuppressWarnings({"all"})
public class SystemUtil {
  /**
   * 将对应类拷贝
   *
   * @param source 待转化bean
   * @param target 目标类class
   * @param <T> 范型
   * @return 返回目标类
   */
  public static <T> T buildResource(Object source, Class<T> target) {
    if (source == null) {
      return null;
    }
    T targetObject = null;
    try {
      targetObject = target.newInstance();
      BeanUtils.copyProperties(source, targetObject);
    } catch (Exception e) {
      log.error("构造对象失败", e);
    }
    return targetObject;
  }

  /**
   * 将对应类拷贝
   *
   * @param sourceList 待转化beanList
   * @param target 目标List范型类class
   * @param <T> 范型
   * @return 返回目标类List
   */
  public static <T> List<T> buildResource(Collection<?> sourceList, Class<T> target) {
    if (sourceList == null) {
      return null;
    }
    List<T> targetList = new ArrayList<>(sourceList.size());

    try {
      for (Object source : sourceList) {
        T targetObject = target.newInstance();
        BeanUtils.copyProperties(source, targetObject);
        targetList.add(targetObject);
      }
    } catch (Exception e) {
      log.error("构造对象失败", e);
    }
    return targetList;
  }

  /**
   * 将对应类拷贝
   *
   * @param iPage 待转化beanPage
   * @param target 目标page对应范型类class
   * @return <T> 返回目标类page
   * @param <T> 范型
   */
  public static <T> Page<T> buildResource(Page<?> iPage, Class<T> target) {
    if (iPage == null) {
      return null;
    }
    List<?> records = iPage.getRecords();
    List<T> ts = buildResource(records, target);

    Page<T> targetPage = new Page<>();
    try {
      BeanUtils.copyProperties(iPage, targetPage);
      targetPage.setRecords(ts);
    } catch (Exception e) {
      log.error("构造对象失败", e);
    }
    return targetPage;
  }

  /**
   * 构建分页DTO
   *
   * @param page
   * @param target
   * @return
   * @param <T>
   * @param <E>
   */
  public static <T, E extends PageDTO> Page<T> buildPageFromDTO(E page, Class<T> target) {
    Page<T> objectPage = new Page<>();
    objectPage.setCurrent(page.getPageNum());
    objectPage.setSize(page.getPageSize());
    return objectPage;
  }

  /**
   * 将Object转化为List
   *
   * @param obj
   * @param clazz
   * @return
   * @param <T>
   */
  public static <T> List<T> object2List(Object obj, Class<T> clazz) {
    List<T> result = new ArrayList<T>();
    if (obj instanceof List<?>) {
      for (Object o : (List<?>) obj) {
        result.add(clazz.cast(o));
      }
      return result;
    }
    return null;
  }

  /**
   * 获取系统标识
   *
   * @return
   */
  public static String detectOS() {
    String os = System.getProperty("os.name").toLowerCase();
    if (os.contains("win")) {
      return "windows";
    } else if (os.contains("linux")) {
      return "linux";
    } else if (os.contains("mac")) {
      return "macOS";
    } else {
      return "Unknown";
    }
  }

  /**
   * 是否是windows系统
   *
   * @return
   */
  public static Boolean isWin() {
    return Objects.equals(detectOS(), SystemConst.Win);
  }

  /**
   * 是否是linux系统
   *
   * @return
   */
  public static Boolean isLinux() {
    return Objects.equals(detectOS(), SystemConst.Linux);
  }

  /**
   * 是否是MacOS系统
   *
   * @return
   */
  public static Boolean isMacOS() {
    return Objects.equals(detectOS(), SystemConst.MacOS);
  }
}
